package io.mft.runtime.function.combination;

import java.util.List;

import io.mft.api.domain.Api;
import io.mft.api.domain.ApiFunctionType;
import io.mft.api.domain.ApiRegulation;
import io.mft.flow.core.FlowExecutor;
import io.mft.flow.core.executable.ConditionType;
import io.mft.flow.core.executable.ExecutableType;
import io.mft.flow.core.executable.builder.ChainBuilder;
import io.mft.flow.core.executable.drive.ChainConditionDriver;
import io.mft.flow.core.executable.drive.ConditionDriverType;
import io.mft.runtime.function.FlowRegister;
import io.mft.runtime.function.combination.component.CombinationApiFunctionExecutorComponent;
import io.mft.runtime.function.combination.component.CombinationApiFunctionProcessComponent;
import io.mft.runtime.function.process.ApiFunctionProcessExecutor;
import io.mft.runtime.function.process.executor.DataAccessProcessExecutor;
import io.mft.runtime.function.process.executor.InputParameterProcessExecutor;
import io.mft.runtime.function.process.executor.LogicProcessExecutor;
import io.mft.runtime.function.process.executor.OutputParameterProcessExecutor;
import io.mft.runtime.function.process.executor.cache.OutputCacheReadAccessProcessExecutor;
import io.mft.runtime.function.process.executor.cache.OutputCacheWriteAccessProcessExecutor;

/**
 * 原子接口编排配置注册
 * 
 * @说明：TODO
 * @类型名称：ApiFunctionProcessExecutorFlowRegister
 * @创建者: 胡常建
 * @创建时间: 2021年2月21日 上午10:04:11
 * @修改者: 胡常建
 * @修改时间: 2021年2月21日 上午10:04:11
 */
public class ApiFunctionProcessExecutorFlowRegister implements FlowRegister{
	private FlowExecutor flowExecutor;
	public ApiFunctionProcessExecutorFlowRegister(FlowExecutor flowExecutor) {
		this.flowExecutor=flowExecutor;
		registerNodeApiFunctionProcessExecutor();
	}
	private void registerNodeApiFunctionProcessExecutor() {
		registerNodeApiFunctionProcessExecutor(DataAccessProcessExecutor.class,false);
		registerNodeApiFunctionProcessExecutor(LogicProcessExecutor.class,false);
		registerNodeApiFunctionProcessExecutor(InputParameterProcessExecutor.class,false);
		registerNodeApiFunctionProcessExecutor(OutputParameterProcessExecutor.class,false);
		registerNodeApiFunctionProcessExecutor(OutputCacheWriteAccessProcessExecutor.class,false);
		registerNodeApiFunctionProcessExecutor(OutputCacheReadAccessProcessExecutor.class,false);
	}


	private <T extends ApiFunctionProcessExecutor<?>> void registerNodeApiFunctionProcessExecutor(Class<T> classType,boolean isReload) {
		flowExecutor.addNode(classType.getSimpleName(), CombinationApiFunctionProcessComponent.class, "",isReload);
	}
	@Override
	public void register(Api api,boolean isReload) {
		if (api.getApiFunctionType() == ApiFunctionType.processExecutor) {
			flowExecutor.addNode(api.getKey(), CombinationApiFunctionExecutorComponent.class, api.getName(),isReload);
			List<ApiRegulation> apiRegulations = api.getApiRegulations();
			String[] nodeIds = new String[apiRegulations.size()];
			int i = 0;
			for (ApiRegulation apiRegulation : apiRegulations) {
				nodeIds[i] = apiRegulation.getRegulation().getProcessClassify().name();
				i++;
			}
			ChainBuilder builder = new ChainBuilder(api.getKey(), flowExecutor);
			ChainConditionDriver driver = ConditionDriverType.noneDeliverReturn.chainConditionDriver();
			builder.addChainConditionExecutable(ExecutableType.NODE,ConditionType.sync, 0, driver,nodeIds).build().addToFlowExecutor(isReload);			
		}		

	}
	@Override
	public void unregister(String apiKey) {
		// TODO Auto-generated method stub
		
	}
}
